/*
 * @Author: 13212657520 10828485+zhaosieve@user.noreply.gitee.com~
 * @Date: 2022-05-30 15:13:44
 * @LastEditors: 13212657520 10828485+zhaosieve@user.noreply.gitee.com~
 * @LastEditTime: 2022-05-30 20:02:15
 * @FilePath: \vue-2202\src\store\modules\car.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const state = {
  count: 100,
  car: JSON.parse(localStorage.getItem("car") || "[]"),
};

const mutations = {
  ADD_CAR(state, shopinnfo) {
    var flag = false;
    state.car.forEach((v) => {
      if (v.id === shopinnfo.id) {
        v.count += shopinnfo.count;
        flag = true;
      }
    });
    if (!flag) {
      state.car.push(shopinnfo);
    }
    localStorage.setItem("car", JSON.stringify(state.car));
  },
};

const actions = {};

const getters = {
  getAllCount(state) {
    var c = 0;
    state.car.forEach((v) => {
      c += v.count;
    });
    return c;
  },
  getAllPice(state) {
    let p = 0;
    state.car.forEach((v) => {
      if (v.completed) {
        p += v.price * v.count * 100;
      }
    });
    return p;
  },
  checkedAll() {
    return state.car.every((v) => v.completed);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
